package weeny.face;


import javax.swing.*;
import javax.swing.plaf.basic.BasicButtonUI;
import java.awt.*;
import java.awt.event.*;

/**
 * Component to be used as tabComponent, containing a JLabel to show the text
 * and a JButton to separate the tab from the pane it belongs to. To use this
 * component, for every tab, you should create a new instance by calling
 * {@link #SeparatableTabComponent(JTabbedPane)}, and add it to the JTabbedPane
 * by calling {@link JTabbedPane#setTabComponentAt(int, Component)} 
 */
class SeparatableTabComponent extends JPanel {
    private final JTabbedPane pane;
    private static Icon ICON = new ImageIcon(
    		SeparatableTabComponent.class.getResource("split_icon.png"));
    public SeparatableTabComponent(final JTabbedPane pane) {
        super(new FlowLayout(FlowLayout.LEFT, 0, 0));
        if (pane == null) {
            throw new NullPointerException("TabbedPane is null");
        }
        this.pane = pane;
        setOpaque(false);
        
        //make JLabel read titles from JTabbedPane
        JLabel label = new JLabel() {
            public String getText() {
                int i = pane.indexOfTabComponent(SeparatableTabComponent.this);
                if (i != -1) {
                    return pane.getTitleAt(i);
                }
                return null;
            }
        };
        add(label);
        //add more space between the label and the button
        label.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 2));
        //tab button
        JButton button = new TabButton();
        add(button);
        //add more space to the top of the component
        setBorder(BorderFactory.createEmptyBorder(2, 0, 0, 0));
    }

    private class TabButton extends JButton implements ActionListener {
        public TabButton() {
            setPreferredSize(new Dimension(10, 12));
            setToolTipText("show in a separated window");
            //Make the button looks the same for all Laf's
            setUI(new BasicButtonUI());
            //Make it transparent
            setContentAreaFilled(false);
            //No need to be focusable
            setFocusable(false);
            setBorder(BorderFactory.createEtchedBorder());
            setBorderPainted(false);
            //Making nice rollover effect
            //we use the same listener for all buttons
            addMouseListener(mouseOverListener);
            setRolloverEnabled(true);
            //Close the proper tab by clicking the button
            addActionListener(this);
            setIcon(ICON);
            setBorder(null);
        }

        public void actionPerformed(ActionEvent e) {
        	int idx = pane.indexOfTabComponent(SeparatableTabComponent.this);
			final JPanel tab = (JPanel) pane.getComponentAt(idx);
			final String tabName = pane.getTitleAt(idx);
			JFrame frame = new JFrame(tabName);
			frame.addWindowListener(new WindowAdapter() {
				@Override
				public void windowClosing(WindowEvent e) {
					pane.add(tabName, tab);
					int newIdx = pane.indexOfTab(tabName);
					Component newTab = new SeparatableTabComponent(pane);
					pane.setTabComponentAt(newIdx, newTab);
					pane.setVisible(true);
					pane.getParent().invalidate();
					pane.getParent().repaint();
				}
			});
			if (idx != -1) {
				pane.remove(idx);
			}
			if (pane.getTabCount() == 0) {
				pane.setVisible(false);
			}
			frame.getContentPane().add(tab, BorderLayout.CENTER);
			frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
			frame.pack();
			frame.setVisible(true);			
		}
        //we don't want to update UI for this button
        public void updateUI() {
        }

        protected void paintComponent(Graphics g) {
        	if (getModel().isRollover()){
        		if (getModel().isPressed()){
            		g.translate(0, 1);
            		super.paintComponent(g);
            		g.translate(0, -1);
            	}
        		else {
        			g.translate(0, -1);
        			super.paintComponent(g);
        			g.translate(0, 1);
        		}
        	}
        	else {
        		super.paintComponent(g);	
        	}
            
//            Graphics2D g2d = (Graphics2D) g.create();
//            //shift the image for pressed buttons
//            if (getModel().isPressed()) {
//                g2d.translate(1, 1);
//            }
//            g2d.setStroke(stroke);
//            int gap = 3;
//            int shift = 3;
//            int width = getWidth()-2*gap-shift;
//            int height = getHeight()-2*gap-shift;
//            g2d.setColor(Color.DARK_GRAY);
//            g2d.fillRect(gap+shift, gap, width, height);
//            g2d.setColor(Color.LIGHT_GRAY);
//            g2d.drawRect(gap, gap+shift, width, height);
//            g2d.dispose();
        }
    }
//    private BasicStroke stroke = new BasicStroke(2);
    private final static MouseListener mouseOverListener = new MouseAdapter() {
        public void mouseEntered(MouseEvent e) {
            Component component = e.getComponent();
            if (component instanceof AbstractButton) {
                AbstractButton button = (AbstractButton) component;
                button.setBorderPainted(true);
            }
        }

        public void mouseExited(MouseEvent e) {
            Component component = e.getComponent();
            if (component instanceof AbstractButton) {
                AbstractButton button = (AbstractButton) component;
                button.setBorderPainted(false);
            }
        }
    };
}